"""定义手机号验证格式"""
import re

from cache.channel import AllChannelsCache


def check_mobile(value):

    if not re.match(r'^1[3-9]\d{9}$', value):
        raise ValueError('手机号格式有误')

    return value


def check_channel_id(value):
    """验证channel_id"""

    try:
        channel_id = int(value)
    except Exception:
        raise ValueError('channel_id 有误')
    else:
        if channel_id < 0:
            raise ValueError('channel_id 有误')
        elif channel_id == 0:
            return value
        else:
            # 判断是否在所有频道范围内
            ret = AllChannelsCache.exists(channel_id)
            if ret:
                return channel_id
            else:
                raise ValueError('Invalid channel id.')

